package com.vm.filter;

import java.io.IOException;


import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.vm.common.WebRootPath;

/**
 * @description 字符串编码
 * @time 2011-08-02
 * @author osnt
 * **/
public class FilterEncoding implements Filter {
	private FilterConfig config;

	public void destroy() {
		this.config = null;

	}

	public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) servletRequest;
		HttpServletResponse response = (HttpServletResponse) servletResponse;
		String coding = config.getInitParameter("coding");
		request.setCharacterEncoding(coding);
		response.setCharacterEncoding(coding);
		chain.doFilter(request, response);

	}

	public void init(FilterConfig config) throws ServletException {
		WebRootPath.setRootPathf(config.getServletContext().getRealPath("/"));
		this.config = config;
	}

}
